﻿using DevComponents.DotNetBar;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VMind.Global;

namespace PPIDesktopClient.Controls.PolygonTree
{
    public partial class PolygonTree : UserControl
    {
        #region Private Members
        private bool m_ExpandAll = false;
        private List<EUtility_Polygon> m_Polygons;
        #endregion

        #region Private Methods
        private void AddNodes()
        {
            try
            {
                tvwMain.Nodes.Add(new TreeNodeEx(Int32.MinValue.ToString(), "Included Areas", GetChildNodes(Int32.MinValue)));
            }
            catch (Exception)
            {

                throw;
            }
        }

        private TreeNodeEx[] GetChildNodes(Int32 Utility_Polygon_ParentPolygonFk)
        {
            List<EUtility_Polygon> _Children = new List<EUtility_Polygon>();
            foreach (EUtility_Polygon _Polygon in m_Polygons)
                if (_Polygon.Utility_Polygon_ParentPolygon.Utility_PolygonId == Utility_Polygon_ParentPolygonFk)
                    _Children.Add(_Polygon);

            TreeNodeEx[] _ChildrenNodes = new TreeNodeEx[_Children.Count];
            for (Int32 i = 0; i < _Children.Count; i++)
                _ChildrenNodes[i] = new TreeNodeEx(_Children[i].Utility_PolygonId.ToString(), _Children[i].Name, GetChildNodes(_Children[i].Utility_PolygonId));
                //_ChildrenNodes[i] = new TreeNode(_Children[i].Utility_PolygonId.ToString(), GetChildNodes(_Children[i].Utility_PolygonId));

            return _ChildrenNodes;
        }
        #endregion

        #region Public Properties
        public bool ExpandAll
        {
            get { return m_ExpandAll; }
            set { m_ExpandAll = value; }
        }
        public List<EUtility_Polygon> Polygons
        {
            get { return m_Polygons; }
            set { m_Polygons = value; }
        }
        #endregion

        #region Public Methods
        public override void Refresh()
        {
            //Suppress repainting the TreeView until all the objects have been created.
            tvwMain.BeginUpdate();
            //Clear the TreeView each time the method is called.
            tvwMain.Nodes.Clear();

            AddNodes();

            if (m_ExpandAll)
                tvwMain.ExpandAll();

            //Begin repainting the TreeView.
            tvwMain.EndUpdate();
        }
        public void SelectPolygons(List<EUtility_Polygon> selectedPolygons)
        {
            //foreach (TreeNodeEx _Node in tvwMain.Nodes)
            //    foreach (EUtility_Polygon _Polygon in selectedPolygons)
            //        if (!_Node.Checked && _Polygon.Utility_PolygonId == int.Parse(_Node.Value))
            //        {
            //            _Node.Checked = true;
            //            m_Polygons.Add(_Polygon);
            //        }
            List<Int32> _SelectedIds = new List<Int32>();
            foreach (EUtility_Polygon _Polygon in selectedPolygons)
                _SelectedIds.Add(_Polygon.Utility_PolygonId);
            SelectPolygons(_SelectedIds);
        }
        public void SelectPolygons(List<Int32> selectedIds)
        {
            tvwMain.BeginUpdate();
            foreach (TreeNodeEx _Node in tvwMain.Nodes)
            {
                if (!_Node.Checked && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value)))
                {
                    _Node.Checked = true;
                    m_Polygons.Add(new EUtility_Polygon(ConvertHelper.ToInt32(_Node.Value)));
                }
                if (_Node.Nodes.Count > 0)
                    CheckChildren(selectedIds, _Node);
            }
            tvwMain.EndUpdate();
        }
        void CheckChildren(List<Int32> selectedIds, TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                if (!_Node.Checked && selectedIds.Contains(ConvertHelper.ToInt32(_Node.Value)))
                {
                    _Node.Checked = true;
                    m_Polygons.Add(new EUtility_Polygon(ConvertHelper.ToInt32(_Node.Value)));
                }
                if (_Node.Nodes.Count > 0)
                    CheckChildren(selectedIds, _Node);
            }
        }
        void CheckChildren(TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                if (!_Node.Checked)
                {
                    _Node.Checked = true;
                }
                if (_Node.Nodes.Count > 0)
                    CheckChildren(_Node);
            }
        }
        #endregion

        #region Constructor
        public PolygonTree()
        {
            InitializeComponent();
            m_Polygons = new List<EUtility_Polygon>();
        }
        #endregion

        #region Events
        public event TreeViewEventHandler CheckedChange;
        private void OnCheckedChanged(EUtility_Polygon child, TreeViewEventArgs e) 
        {
            // Safely invoke an event:
            if (CheckedChange != null)
                CheckedChange(child, e);
        }


        void tvwMain_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            foreach (EUtility_Polygon _Polygon in m_Polygons)
                if (_Polygon.Utility_PolygonId == ConvertHelper.ToInt32(((TreeNodeEx)e.Node).Value))
                {
                    OnCheckedChanged(_Polygon, e);
                    CheckChildren((TreeNodeEx)e.Node);
                }
                    
        }
        #endregion

    }
}
